Page({
  data: {
    rectChoices: [{
        rectName: '天气',
        backgroundSrc: "https://www.testmu.cn/img/tq.jpg"
      },
      {
        rectName: '欢迎语',
        backgroundSrc: "https://www.testmu.cn/img/welcome.jpg"
      },
      {
        rectName: '微博热搜',
        backgroundSrc: "https://www.testmu.cn/img/wb.jpg"
      },
      {
        rectName: '古时今日',
        backgroundSrc: "https://www.testmu.cn/img/gu.jpg"
      }
    ],
    inputWeather: '',
    inputEmail: ''
  },

  // 登录接口调用
  async login() {
    const url = 'https://www.testmu.cn:8000/login';
    const data = {
      username: 'jy',
      password: '123456',
    };

    try {
      const response = wx.request({
        url: url,
        method: 'POST',
        data: data,
        success: (res) => {
          if (res.statusCode === 200) {
            // 获取返回数据
            const resData = res.data;
            // 解析JSON
            const parsedData = JSON.parse(JSON.stringify(res.data));
            const access_token = parsedData.token;
            const msg = parsedData.msg;
            const code = parsedData.code;
            // console.log("token =", access_token);
            // console.log("ret = ", msg);
            // console.log("code = ", code);
            this.setData({
              access_token
            });
            // 使用访问令牌进行邮件发送
            this.sendEmail(access_token);
          } else {
            wx.showToast({
              title: '登录失败',
              icon: 'none',
              duration: 2000,
            });
          }
        },
        fail: (err) => {
          wx.showToast({
            title: '登录网络错误',
            icon: 'none',
            duration: 2000,
          });
        },
      });
    } catch (error) {
      wx.showToast({
        title: '登录请求失败',
        icon: 'none',
        duration: 2000,
      });
    }
  },

  // 邮件发送接口调用
  async sendEmail(access_token) {
    const url = 'https://www.testmu.cn:8000/send';
    const data = {
      module: "0,1,2,3",
      city: this.data.inputWeather,
      emailnum: this.data.inputEmail
    };
    if (this.data.inputWeather === '' || this.data.inputEmail === '') {
      wx.showToast({
        title: '请输入查询城市和邮件名',
        icon: 'none',
        duration: 2000,
      });
      return;
    }
    try {
      const response = wx.request({
        url: url,
        method: 'POST',
        data: data,
        header: {
          'Authorization': access_token
        },
        success: (res) => {
          if (res.statusCode === 200) {
            wx.showToast({
              title: '邮件发送成功',
              icon: 'success',
              duration: 2000,
            });
          } else {
            // console.log("res status = ", res.statusCode);
            wx.showToast({
              title: '邮件发送失败',
              icon: 'none',
              duration: 2000,
            });
          }
        },
        fail: (err) => {
          wx.showToast({
            title: '发送请求失败',
            icon: 'none',
            duration: 2000,
          });
        },
      });
    } catch (error) {
      wx.showToast({
        title: '发送请求失败',
        icon: 'none',
        duration: 2000,
      });
    }
  },

  onInputWeatherChanged: function (e) {
    this.setData({
      inputWeather: e.detail.value
    });
  },
  onInpoutEmailChanged: function (e) {
    this.setData({
      inputEmail: e.detail.value
    })
  }
});